package com.nerve.cloudoffice.web.action;

import java.util.List;

import com.nerve.cloudoffice.common.Reply;
import com.nerve.cloudoffice.domain.Department;
import com.nerve.cloudoffice.domain.bean.DepartmentTask;
import com.nerve.cloudoffice.service.DepartmentService;
import com.nerve.cloudoffice.service.impl.DepartmentServiceImpl;

/**
 * @project cloudOffice DepartmentAction.java
 * @datetime Jul 30, 2013 4:07:05 AM
 * @description 
 * 	组织管理   
 * @author 集成显卡 IBM 
 *			zxingming@qq.com
 *			zengxm@cn.ibm.com
 *
 * @change log:
 *
 */
@SuppressWarnings("serial")
public class DepartmentAction extends NerveAction {

	private DepartmentService service = new DepartmentServiceImpl();
	
	private int id;
	private String name;
	
	public void list(){
		Reply re = new Reply();
		try{
			List<Department> list = service.listAll();
			
			re.setError(false);
			re.setMessage(list);
		}catch(Exception e){
			re.error(e);
		}
		json(re);
	}
	
	/**
	 * 新增或者更新部门
	 * 如果id为0，则为新增
	 * 不为0，则更新
	 */
	public void add(){
		Reply re = new Reply();
		try{
			Department d = new Department();
			d.setId(id);
			d.setName(name);
			
			int lastId = service.save(d);
			log("add new department[id="+id+", name="+name+"]");
			re.setMessage(lastId);
		}catch(Exception e){
			e.printStackTrace();
			error("add department error[name="+name+"]：" + e.getMessage());
			
			re.error(e);
		}
		
		json(re);
	}
	
	public void delete(){
		Reply re = new Reply();
		try{
			service.delete(id);
			log("delete department[id="+id+"]");
		}catch(Exception e){
			e.printStackTrace();
			error("delete department error[id="+id+"]:"+e.getMessage());
			
			re.error(e);
		}
		
		json(re);
	}
	
	/**
	 * @method name: countTask
	 * @description: 计算部门任务数
	 * @return type: void
	 */
	public void countTask(){
		Reply re = new Reply();
		
		try{
			List<DepartmentTask> dtList = service.countTask();
			re.setMessage(dtList);
		}catch(Exception e){
			e.printStackTrace();
			
			re.error(e);
		}
		
		json(re);
	}

	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
}